package com.ssy.lingxi.pay.model.allInPayModel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ssy.lingxi.pay.utils.IDGenerator;
import com.ssy.lingxi.pay.utils.RSAUtil;
import lombok.extern.slf4j.Slf4j;

import static com.ssy.lingxi.pay.model.allInPayModel.YunClient.encodeOnce;

@Slf4j
public class MemberServiceUtil {

    /**
     * 创建会员接口
     *
     * @param bizUserId  商户系统用户标识,商户系统中唯一编号
     * @param memberType 会员类型
     * @param source     登录端
     * @return 请求结果
     */
    public static String createMember(String bizUserId, Integer memberType, Integer source, String cacheParams,String url) throws Exception {
        final YunRequest request = new YunRequest("MemberService", "createMember");
        if ("auto".equalsIgnoreCase(bizUserId)) {
            bizUserId = IDGenerator.generateGUID();
        }
        request.put("bizUserId", bizUserId);
        request.put("memberType", memberType);
        request.put("source", source);
        log.info("[bizUserId=" + bizUserId + ", memberType=3" + ", source=2]");
        return YunClient.request(request, cacheParams,url);
    }

    /**
     * 发送短信验证码
     *
     * @param bizUserId            商户系统用户标识,商户系统中唯一编号
     * @param phone                手机号码
     * @param verificationCodeType 9-绑定手机 6-解绑手机
     * @return 操作结果
     */
    public static String sendVerificationCode(String bizUserId, String phone, Integer verificationCodeType, String cacheParams,String url) throws Exception {
        final YunRequest request = new YunRequest("MemberService", "sendVerificationCode");
        if ("auto".equalsIgnoreCase(bizUserId)) {
            bizUserId = IDGenerator.generateGUID();
        }
        request.put("bizUserId", bizUserId);
        request.put("phone", phone);
        request.put("verificationCodeType", verificationCodeType);
        log.info("[bizUserId=" + bizUserId + ", memberType=3" + ", source=2]");
        return YunClient.request(request, cacheParams,url);
    }

    /**
     * 绑定手机  验证码需要通过发送短信的接口触发，然后调用绑定手机接口
     *
     * @param bizUserId        商户系统用户标识,商户系统中唯一编号
     * @param phone            手机号码
     * @param verificationCode 验证码
     */

    public static String bindPhone(String bizUserId, String phone, String verificationCode, String cacheParams,String url) throws Exception {
        final YunRequest request = new YunRequest("MemberService", "bindPhone");
        request.put("bizUserId", bizUserId);
        request.put("phone", phone);
        request.put("verificationCode", verificationCode);
        return YunClient.request(request, cacheParams,url);

    }

    /**
     * 个人实名
     *
     * @param bizUserId    商户系统用户标识,商户系统中唯一编号
     * @param name         姓名
     * @param identityType 身份证类型
     * @param identityNo   身份证号码
     */
    public static String setRealName(String bizUserId, String name, String identityType, String identityNo, String cacheParams,String url) throws Exception {
        final YunRequest request = new YunRequest("MemberService", "setRealName");
        request.put("bizUserId", bizUserId);
        request.put("isAuth", "true");
        request.put("name", name);
        request.put("identityType", identityType);
        request.put("identityNo", RSAUtil.encrypt(identityNo, cacheParams));
        return YunClient.request(request, cacheParams,url);
    }

    /**
     * 会员电子协议签约
     *
     * @param bizUserId  商户id
     * @param backUrl    回调地址
     * @param source     登录端
     * @param gateWayUrl 网关地址
     * @return 操作结果
     * @throws Exception 异常
     */
    public static String signContract(String bizUserId, Long jumpPageType, String backUrl, Integer source, String gateWayUrl, String cacheParams) throws Exception {
        final YunRequest request = new YunRequest("MemberService", "signContract");
        request.put("bizUserId", bizUserId);
        request.put("jumpPageType", jumpPageType);
        request.put("backUrl", backUrl);
        request.put("source", source);//1-Mobile 2-PC,此属性不影响登录端是app还是pc
        String param = encodeOnce(request, cacheParams);
        return gateWayUrl + "?" + param;
    }

    /**
     * 会员电子协议签约查询
     *
     * @param bizUserId    商户id
     * @param jumpPageType 跳转类型
     * @param source       登录端
     * @param gateWayUrl   网关地址
     * @return 操作结果
     */
    public static String signContractQuery(String bizUserId, Long jumpPageType, Integer source, String gateWayUrl, String cacheParams) throws Exception {
        final YunRequest request = new YunRequest("MemberService", "signContractQuery");
        request.put("bizUserId", bizUserId);
        request.put("jumpPageType", jumpPageType);
        request.put("source", 2);//1-Mobile 2-PC,此属性不影响登录端是app还是pc
        String param = encodeOnce(request, cacheParams);
        return gateWayUrl + "?" + param;
    }

    public static String getMemberInfo(String bizUserId, String cacheParams,String url) throws Exception {
        final YunRequest request = new YunRequest("MemberService", "getMemberInfo");
        request.put("bizUserId", bizUserId);
        return YunClient.request(request, cacheParams, url);
    }

    /**
     * 申请绑定银行卡
     *
     * @param bizUserId 当前登录用户
     */
    public static String applyBindBankCard(String bizUserId, Integer cardCheck, String cardPhone,
                                           String cardNo, String name, Integer identityType,
                                           String identityNo, String validate, String cvv2, Boolean isSafeCard, String bindCardUnionBank, String cacheParams,String url) throws
            Exception {
        final YunRequest request = new YunRequest("MemberService", "applyBindBankCard");
        request.put("bizUserId", bizUserId);
        if (cardCheck != 1) {   //绑卡方式 1-通联通三要素绑 卡，手机号可不上送 其他绑卡方式必须上送
            request.put("phone", cardPhone);
        }
        request.put("cardCheck", cardCheck);
        request.put("cardNo", RSAUtil.encrypt(cardNo, cacheParams));
        request.put("name", name);
        request.put("identityNo", RSAUtil.encrypt(identityNo, cacheParams));
        request.put("identityType", identityType);
        return YunClient.request(request, cacheParams,url);
    }


    public static String bindBankCard(String bizUserId, String tranceNum, String transDate,
                                      String phone, String verificationCode, String cacheParams,String url) throws Exception {
        final YunRequest request = new YunRequest("MemberService", "bindBankCard");
        request.put("bizUserId", bizUserId);
        request.put("tranceNum", tranceNum);
        request.put("phone", phone);
        request.put("verificationCode", verificationCode);
        return YunClient.request(request, cacheParams,url);
    }


/*    public static void setSafeCard(String bizUserId, String cardNo, boolean safeCard) {
        log.info("================MemberAPI: setSafeCard begin================");
        long start = System.currentTimeMillis();
        //HashMap<String, String> result = new HashMap<String, String>();
        final YunRequest request = new YunRequest("MemberService", "setSafeCard");
        try {
            request.put("bizUserId", bizUserId);
            request.put("cardNo", RSAUtil.encrypt(cardNo));
            request.put("setSafeCard", safeCard);
            String res = YunClient.request(request);

            JSONObject resp = JSON.parseObject(res);
//			System.out.println("status=[" + resp.getString("status") + "]");
//			System.out.println("signedValue=[" + resp.getString("signedValue") + "]");
//			System.out.println("sign=[" + resp.getString("sign") + "]");
//			System.out.println("errorCode=[" + resp.getString("errorCode") + "]");
//			System.out.println("message=[" + resp.getString("message") + "]");

            String status = resp.getString("status");
            if (status != null && status.equals("OK")) {
                log.info("API[setSafeCard SUCCESS!]");

            } else {
                log.error("API_ERROR[setSafeCard RESULT=[" + resp.getString("errorCode") + ", " + resp.getString("message") + "]]");
            }

        } catch (final Exception e) {
            log.error("EXCEPTION", e);
        }

        long end = System.currentTimeMillis();
        long elapse = end - start;
        log.info("================MemberAPI: setSafeCard end================[elapse: " + elapse + " ms]");
        //return result;
    }*/


  /*  public static void queryBankCard(String bizUserId, String cardNo) {
        log.info("================MemberAPI: queryBankCard begin================");
        long start = System.currentTimeMillis();
        //HashMap<String, String> result = new HashMap<String, String>();
        final YunRequest request = new YunRequest("MemberService", "queryBankCard");
        try {
            request.put("bizUserId", bizUserId);
            request.put("cardNo", RSAUtil.encrypt(cardNo));

            String res = YunClient.request(request);

            JSONObject resp = JSON.parseObject(res);
//			System.out.println("status=[" + resp.getString("status") + "]");
//			System.out.println("signedValue=[" + resp.getString("signedValue") + "]");
//			System.out.println("sign=[" + resp.getString("sign") + "]");
//			System.out.println("errorCode=[" + resp.getString("errorCode") + "]");
//			System.out.println("message=[" + resp.getString("message") + "]");

            String status = resp.getString("status");
            if (status != null && status.equals("OK")) {
                log.info("API[queryBankCard SUCCESS!]");

            } else {
                log.error("API_ERROR[queryBankCard RESULT=[" + resp.getString("errorCode") + ", " + resp.getString("message") + "]]");
            }

        } catch (final Exception e) {
            log.error("EXCEPTION", e);
        }

        long end = System.currentTimeMillis();
        long elapse = end - start;
        log.info("================MemberAPI: queryBankCard end================[elapse: " + elapse + " ms]");
        //return result;

    }*/


    public static String unbindBankCard(String bizUserId, String cardNo, String cacheParams,String url) throws Exception {
        final YunRequest request = new YunRequest("MemberService", "unbindBankCard");
        request.put("bizUserId", bizUserId);
        request.put("cardNo", RSAUtil.encrypt(cardNo, cacheParams));
        return YunClient.request(request, cacheParams,url);
    }


  /*  public static void lockMember(String bizUserId) {
        log.info("================MemberAPI: lockMember begin================");
        long start = System.currentTimeMillis();
        //HashMap<String, String> result = new HashMap<String, String>();
        final YunRequest request = new YunRequest("MemberService", "lockMember");
        try {
            request.put("bizUserId", bizUserId);

            String res = YunClient.request(request);

            JSONObject resp = JSON.parseObject(res);
//			System.out.println("status=[" + resp.getString("status") + "]");
//			System.out.println("signedValue=[" + resp.getString("signedValue") + "]");
//			System.out.println("sign=[" + resp.getString("sign") + "]");
//			System.out.println("errorCode=[" + resp.getString("errorCode") + "]");
//			System.out.println("message=[" + resp.getString("message") + "]");

            String status = resp.getString("status");
            if (status != null && status.equals("OK")) {
                log.info("API[lockMember SUCCESS!]");

            } else {
                log.error("API_ERROR[lockMember RESULT=[" + resp.getString("errorCode") + ", " + resp.getString("message") + "]]");
            }

        } catch (final Exception e) {
            log.error("EXCEPTION", e);
        }

        long end = System.currentTimeMillis();
        long elapse = end - start;
        log.info("================MemberAPI: lockMember end================[elapse: " + elapse + " ms]");
        //return result;

    }*/

  /*  public static void unlockMember(String bizUserId) {
        log.info("================MemberAPI: unlockMember begin================");
        long start = System.currentTimeMillis();
        //HashMap<String, String> result = new HashMap<String, String>();
        final YunRequest request = new YunRequest("MemberService", "unlockMember");
        try {
            request.put("bizUserId", bizUserId);

            String res = YunClient.request(request);

            JSONObject resp = JSON.parseObject(res);
//			System.out.println("status=[" + resp.getString("status") + "]");
//			System.out.println("signedValue=[" + resp.getString("signedValue") + "]");
//			System.out.println("sign=[" + resp.getString("sign") + "]");
//			System.out.println("errorCode=[" + resp.getString("errorCode") + "]");
//			System.out.println("message=[" + resp.getString("message") + "]");

            String status = resp.getString("status");
            if (status != null && status.equals("OK")) {
                log.info("API[unlockMember SUCCESS!]");

            } else {
                log.error("API_ERROR[unlockMember RESULT=[" + resp.getString("errorCode") + ", " + resp.getString("message") + "]]");
            }

        } catch (final Exception e) {
            log.error("EXCEPTION", e);
        }

        long end = System.currentTimeMillis();
        long elapse = end - start;
        log.info("================MemberAPI: unlockMember end================[elapse: " + elapse + " ms]");
        //return result;
    }*/

 /*   public static void vspTermidService(String bizUserId, String operationType, String vspMerchantid, String
            appid, String vspTermid) {
        log.info("================MemberAPI: vspTermidService begin================");
        long start = System.currentTimeMillis();
        //HashMap<String, String> result = new HashMap<String, String>();
        final YunRequest request = new YunRequest("MemberService", "vspTermidService");
        try {
            request.put("bizUserId", bizUserId);
            request.put("operationType", operationType);
            request.put("vspMerchantid", vspMerchantid);
            request.put("appid", appid);
            request.put("vspTermid", vspTermid);
            String res = YunClient.request(request);

            JSONObject resp = JSON.parseObject(res);
//			System.out.println("status=[" + resp.getString("status") + "]");
//			System.out.println("signedValue=[" + resp.getString("signedValue") + "]");
//			System.out.println("sign=[" + resp.getString("sign") + "]");
//			System.out.println("errorCode=[" + resp.getString("errorCode") + "]");
//			System.out.println("message=[" + resp.getString("message") + "]");

            String status = resp.getString("status");
            if (status != null && status.equals("OK")) {
                log.info("API[vspTermidService SUCCESS!]");

            } else {
                log.error("API_ERROR[vspTermidService RESULT=[" + resp.getString("errorCode") + ", " + resp.getString("message") + "]]");
            }

        } catch (final Exception e) {
            log.error("EXCEPTION", e);
        }

        long end = System.currentTimeMillis();
        long elapse = end - start;
        log.info("================MemberAPI: vspTermidService end================[elapse: " + elapse + " ms]");

    }*/

 /*   public static void setPayPwd() {

        // 宁波商户接入环境
        //String webParamUrl = "http://122.227.225.142:23661/pwd/setPayPwd.html?";

        // 上海商户接入环境
        String webParamUrl = "http://192.168.14.70:8080/pwd/setPayPwd.html?";

        final YunRequest request = new YunRequest("MemberPwdService", "setPayPwd");

        try {
            request.put("bizUserId", "2018072001");
            request.put("phone", "13800138002");
            request.put("name", "梅西");
            request.put("identityType", 1L);
            request.put("identityNo", RSAUtil.encrypt("130124198908210051"));
            request.put("jumpUrl", "http://122.227.225.142:23663/testFront.jsp");
            request.put("backUrl", "http://122.227.225.142:23663/testFront.jsp");

            String res = YunClient.encodeTwice(request);
            webParamUrl += res;
            System.out.println("webParamUrl: " + webParamUrl);

        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
*/
    /**
     * 解绑手机
     *
     * @param bizUserId        商户id
     * @param phone            原手机
     * @param verificationCode 验证码
     * @return 操作结果
     */
    public static String unbindPhone(String bizUserId, String phone, String verificationCode, String cacheParams,String url) throws Exception {
        final YunRequest request = new YunRequest("MemberService", "unbindPhone");
        request.put("bizUserId", bizUserId);
        request.put("phone", phone);
        request.put("verificationCode", verificationCode);
        return YunClient.request(request, cacheParams,url);
    }

    /**
     * 设置企业信息
     *
     * @param bizUserId        商户id
     * @param companyBasicInfo 公司基本信息
     * @param isAuth           是否验证
     * @return 操作结果
     */
    public static String setCompanyInfo(String bizUserId, JSONObject companyBasicInfo, Boolean isAuth, String cacheParams,String url) throws
            Exception {
        final YunRequest request = new YunRequest("MemberService", "setCompanyInfo");
        request.put("bizUserId", bizUserId);
        request.put("companyBasicInfo", companyBasicInfo);
        request.put("isAuth", isAuth);
        return YunClient.request(request, cacheParams,url);

    }


    /**
     * 影印件采集认证
     *
     * @param bizUserId                  商户id
     * @param picType                    影印件类型
     * @param picture                    影印件
     * @param ocrComparisonResultBackUrl 异步回调通知地址
     * @return 上传结果
     * @throws Exception 异常
     */
    public static String idCardCollect(String bizUserId, Integer picType, String picture, String ocrComparisonResultBackUrl, String cacheParams,String url) throws Exception {
        final YunRequest request = new YunRequest("MemberService", "idcardCollect");
        request.put("bizUserId", bizUserId);
        request.put("picType", picType);
        request.put("picture", RSAUtil.base64EncodeData(picture, cacheParams));
        return YunClient.request(request, cacheParams,url);
    }

/*    *//**
     * 通联支付-企业会员绑定对公户
     *
     * @param bizUserId  当前登录id
     * @param accountNo  企业对公账户
     * @param bankName   开户银行名称
     * @param branchName 开户行支行名称
     * @param bankNo     支付行号
     * @return 操作结果
     *//*
    public static String bindCompanyAccount(String bizUserId, String accountNo, String bankName, String branchName, String bankNo) throws Exception {
        final YunRequest request = new YunRequest("MemberService", "bindCompanyAccount");
        request.put("bizUserId", bizUserId);
        request.put("accountNo", RSAUtil.encrypt(accountNo));
        request.put("parentBankName", bankName);
        request.put("bankName", branchName);
        request.put("unionBank", bankNo);
        return YunClient.request(request);
    }*/

    /**
     * 通联支付-会员绑定支付账户用户标识
     *
     * @param bizUserId     商户id
     * @param operationType 操作类型
     * @param acctType      支付账户类型
     * @param acct          支付账户标识
     * @return 绑定结果
     */
    public static String applyBindAcct(String bizUserId, String operationType, String acctType, String acct, String cacheParams,String url) throws Exception {
        final YunRequest request = new YunRequest("MemberService", "applyBindAcct");
        request.put("bizUserId", bizUserId);
        request.put("operationType", operationType);
        request.put("acctType", acctType);
        request.put("acct", acct);
        return YunClient.request(request, cacheParams,url);
    }
}
